﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class client_contact_service : base_service
    {
        //get all get all
        public PagedList<client_contact> client_contact_plist(bool? is_read, int pageindex, int pagesize)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_client_contact
                            where (dt.is_read == is_read || is_read == null)
                            select dt);
                list = list.OrderByDescending(p => p.is_read).ThenByDescending(p => p.contact_id);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        //get list client_contact 
        public List<client_contact> client_contact_list(bool is_read)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_client_contact
                            where (dt.is_read == is_read || is_read == null)
                            select dt);
                list = list.OrderByDescending(p => p.is_read).ThenByDescending(p => p.contact_id);
                return list.ToList();
            }
        }

        public long update(client_contact it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_client_contact.Where(p => p.contact_id == it.contact_id).FirstOrDefault();
                if (item != null)
                {
                    item.contact_id = it.contact_id;
                    item.contact_name = it.contact_name;
                    item.contact_email = it.contact_email;
                    item.contact_phone = it.contact_phone;
                    item.contact_address = it.contact_address;
                    item.title = it.title;
                    item.content_full = it.content_full;
                   // item.sent_date = Udate_time.GetCurrentDateTime();
                    item.is_read = it.is_read;

                    data.SaveChanges();
                    return item.contact_id;
                }
                else
                {
                    data.AddTotbl_client_contact(it);
                    data.SaveChanges();
                    return it.contact_id;
                }
            }
        }

        //cap nhat trang thai
        public long updateStatus(long id, bool is_read)
        {
            long rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_client_contact.Where(p => p.contact_id == id).SingleOrDefault();

                if (edit_item != null)
                {
                    edit_item.is_read = is_read;
                    rt = update(edit_item);
                }
            } return rt;
        }

        //get items by id or code
        public client_contact getByID(long id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_client_contact.Where(p => p.contact_id == id).FirstOrDefault();
               // if (item == null)
                //    item = new client_contacts();
                return item;
            }
        }

        //delete by id
        public void deleteItem(long id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_client_contact.Where(p => p.contact_id == id).FirstOrDefault();

                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

    }
}
